﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class SupplierListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Supplier dbcus = new Supplier();
        public SupplierListForm()
        {
            InitializeComponent();
        }

        private void SupplierListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }

        #region functions
        private void RefreshDB()
        {
            var sup = (from s in dbcontext.Suppliers
                       select s).ToList();

            LVSupplierListInfo.Items.Clear();
            if (sup != null)
            {

                foreach (var sqlSup in sup)
                {
                    ListViewItem lvitems = LVSupplierListInfo.Items.Add(sqlSup.Supplierid_PK.ToString());
                    lvitems.SubItems.Add(sqlSup.Supplier_Name.ToString());
                    lvitems.SubItems.Add(sqlSup.Address.ToString());
                    lvitems.SubItems.Add(sqlSup.City.ToString());
                    lvitems.SubItems.Add(sqlSup.Description.ToString());
                    lvitems.SubItems.Add(sqlSup.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlSup.Contacts.ToString());

                }
            }
            this.Refresh();
        }



        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion

        private void LVSupplierListInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //var sup = (from s in dbcontext.Suppliers
            //           where s.Supplier_Name == txtSearch.Text
            //           select s).ToList();

            //LVSupplierListInfo.Items.Clear();
            //if (sup != null)
            //{

            //    foreach (var sqlSup in sup)
            //    {
            //        ListViewItem lvitems = LVSupplierListInfo.Items.Add(sqlSup.Supplierid_PK.ToString());
            //        lvitems.SubItems.Add(sqlSup.Supplier_Name.ToString());
            //        lvitems.SubItems.Add(sqlSup.Address.ToString());
            //        lvitems.SubItems.Add(sqlSup.City.ToString());
            //        lvitems.SubItems.Add(sqlSup.Description.ToString());
            //        lvitems.SubItems.Add(sqlSup.Email_Address.ToString());
            //        lvitems.SubItems.Add(sqlSup.Contacts.ToString());

            //    }
            //    RefreshDB();
            //}
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var sup = (from s in dbcontext.Suppliers    
                       where s.Supplier_Name.Contains(txtSearch.Text)
                       select s).ToList();

            LVSupplierListInfo.Items.Clear();
            if (sup != null)
            {

                foreach (var sqlSup in sup)
                {
                    ListViewItem lvitems = LVSupplierListInfo.Items.Add(sqlSup.Supplierid_PK.ToString());
                    lvitems.SubItems.Add(sqlSup.Supplier_Name.ToString());
                    lvitems.SubItems.Add(sqlSup.Address.ToString());
                    lvitems.SubItems.Add(sqlSup.City.ToString());
                    lvitems.SubItems.Add(sqlSup.Description.ToString());
                    lvitems.SubItems.Add(sqlSup.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlSup.Contacts.ToString());

                }

                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

    }
}
